﻿using Microsoft.Xna.Framework;
using System.Collections.Generic;

namespace Babylon.Primitives
{
    /// <summary>
    /// Textured cubes with tangent information
    /// </summary>
    public class TexturedCubeWithTangents : CubeBase<VertexPositionNormalTextureTangent>
    {
        /// <summary>
        /// creates an instance of a Textured cube with tangents
        /// </summary>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="depth"></param>
        public TexturedCubeWithTangents(float width, float height, float depth)
            :base(width, height, depth)
        { }
        protected override void BeforeCreatingVertexArray()
        {
        }

        protected override VertexPositionNormalTextureTangent CreateVertex(Microsoft.Xna.Framework.Vector3 position, Microsoft.Xna.Framework.Vector3 normal)
        {
            return new VertexPositionNormalTextureTangent { Position = position, Normal = normal };
        }

        protected override void AfterCreatingVertexArray(VertexPositionNormalTextureTangent[] vertices)
        {
            Vector2[] texCoords = new Vector2[] { new Vector2(0, 0), new Vector2(1, 0), new Vector2(0, 1), new Vector2(1, 1) };
            Dictionary<Vector3, Vector3> _tangentsByNormal = new Dictionary<Vector3, Vector3> { 
                 {Vector3.Backward, Vector3.Right},
                 {Vector3.Up, Vector3.Right},
                 {Vector3.Down, Vector3.Right},
                 {Vector3.Right, Vector3.Forward},
                 {Vector3.Left, Vector3.Backward},
                 {Vector3.Forward, Vector3.Left}
            };

            for (int i = 0; i < vertices.Length; i++)
            {
                vertices[i].TexCoords = texCoords[i % texCoords.Length];
                vertices[i].Tangent = _tangentsByNormal[vertices[i].Normal];
            }
        }

        protected override Microsoft.Xna.Framework.Graphics.VertexDeclaration GetVertexDeclaration()
        {
            return VertexPositionNormalTextureTangent.GetVertexDeclaration();
        }
    }
}
